using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SharpCore.Data;
using SharpCore.Extensions;
using SharpCore.Utilities;

namespace MMS
{
	public class CustomerDAO
	{
		#region Fields

		private string connectionStringName;

		#endregion

		#region Constructors

		public CustomerDAO(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the Customer table.
		/// </summary>
		public void Insert(CustomerDTO customer)
		{
			ValidationUtility.ValidateArgument("customer", customer);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@name", customer.Name),
				new SqlParameter("@address", customer.Address),
				new SqlParameter("@active", customer.Active),
				new SqlParameter("@email", customer.Email),
				new SqlParameter("@website", customer.Website),
				new SqlParameter("@phone1", customer.Phone1),
				new SqlParameter("@phone2", customer.Phone2)
			};

			customer.Id = (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "CustomerInsert", parameters);
		}

		/// <summary>
		/// Updates a record in the Customer table.
		/// </summary>
		public void Update(CustomerDTO customer)
		{
			ValidationUtility.ValidateArgument("customer", customer);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", customer.Id),
				new SqlParameter("@name", customer.Name),
				new SqlParameter("@address", customer.Address),
				new SqlParameter("@active", customer.Active),
				new SqlParameter("@email", customer.Email),
				new SqlParameter("@website", customer.Website),
				new SqlParameter("@phone1", customer.Phone1),
				new SqlParameter("@phone2", customer.Phone2)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CustomerUpdate", parameters);
		}

		/// <summary>
		/// Deletes a record from the Customer table by its primary key.
		/// </summary>
		public void Delete(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CustomerDelete", parameters);
		}

		/// <summary>
		/// Selects a single record from the Customer table.
		/// </summary>
		public CustomerDTO Select(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CustomerSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects a single record from the Customer table.
		/// </summary>
		public string SelectJson(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "CustomerSelect", parameters);
		}

		/// <summary>
		/// Selects all records from the Customer table.
		/// </summary>
		public List<CustomerDTO> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CustomerSelectAll"))
			{
				List<CustomerDTO> customerDTOList = new List<CustomerDTO>();
				while (dataReader.Read())
				{
					CustomerDTO customerDTO = MapDataReader(dataReader);
					customerDTOList.Add(customerDTO);
				}

				return customerDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the Customer table.
		/// </summary>
		public string SelectAllJson()
		{
			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "CustomerSelectAll");
		}

		/// <summary>
		/// Creates a new instance of the CustomerDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private CustomerDTO MapDataReader(SqlDataReader dataReader)
		{
			CustomerDTO customerDTO = new CustomerDTO();
			customerDTO.Id = dataReader.GetInt32("id", 0);
			customerDTO.Name = dataReader.GetString("name", null);
			customerDTO.Address = dataReader.GetString("address", null);
			customerDTO.Active = dataReader.GetBoolean("active", false);
			customerDTO.Email = dataReader.GetString("email", String.Empty);
			customerDTO.Website = dataReader.GetString("website", String.Empty);
			customerDTO.Phone1 = dataReader.GetString("phone1", String.Empty);
			customerDTO.Phone2 = dataReader.GetString("phone2", String.Empty);

			return customerDTO;
		}

		#endregion
	}
}
